﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Block : MonoBehaviour
{
    [SerializeField]
    Button selfButton;
    [SerializeField]
    Image img;
    [SerializeField]
    Text num;

    [SerializeField]
    Sprite NormalBlock;
    [SerializeField]
    Sprite EmptyBlock;
    [SerializeField]
    Sprite MineBomb;
    [SerializeField]
    Sprite MineFlag;
    [SerializeField]
    Sprite Mine;

    System.Action<int> onClickCallback = null; 

    public enum BlockState
    {
        Normal,
        Empty,
        Num,
        Flag,
        Bomb,
        Mine,
    }
    BlockState m_state = BlockState.Normal;
    public BlockState myState { get { return m_state; } }

    int m_index = 0;

    public void Reset(int _index, System.Action<int> _callback)
    {
        m_index = _index;
        onClickCallback = _callback;
        SetState(BlockState.Normal);
    }

    public void SetState(BlockState newState, int showNum = 0)
    {
        m_state = newState;
        num.gameObject.SetActive(BlockState.Num == newState);
        switch (newState)
        {
            case BlockState.Normal:
                img.sprite = NormalBlock;
                break;
            case BlockState.Empty:
                img.sprite = EmptyBlock;
                break;
            case BlockState.Num:
                img.sprite = EmptyBlock;
                num.text = showNum.ToString();
                break;
            case BlockState.Flag:
                img.sprite = MineFlag;
                break;
            case BlockState.Bomb:
                img.sprite = MineBomb;
                break;
            case BlockState.Mine:
                img.sprite = Mine;
                break;
        }
    }

    public void Show()
    {
        gameObject.SetActive(true);
    }

    public void Hide()
    {
        gameObject.SetActive(false);
    }

    public void OnClick()
    {
        if (onClickCallback != null)
        {
            onClickCallback(m_index);
        }
    }
}
